const Router = require('koa-router');
const addCommentPraise = new Router();
const sqldata = require('./sqldata');
addCommentPraise.post('/',async(ctx)=>{
    let sql;
    let val=ctx.request.body;
    let mydata;
    let mydata1;
    console.log(val);
    if(val.img=='https://s3.bmp.ovh/imgs/2021/11/2d7cde86aa7b7457.png'){
        mydata = await new Promise((resolve, reject)=>{
            sql=`update comment set Czan=Czan+1 where Cid='${val.Cid}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
        mydata1 = await new Promise((resolve, reject)=>{
            sql=`insert into commentfabulous values('${val.Cid}','${val.Uid}','${val.Ctime}')`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }else{
        mydata = await new Promise((resolve, reject)=>{
            sql=`update comment set Czan=Czan-1 where Cid='${val.Cid}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
        mydata1 = await new Promise((resolve, reject)=>{
            sql=`delete from commentfabulous where Cid='${val.Cid}' and Uid='${val.Uid}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }
})
module.exports = addCommentPraise;